package com.ann.test;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ParseAnn {

	public static void main(String[] args) {
		//1.使用类加载器加载类
		try {
			Class c = Class.forName("com.ann.test.Child");
			//找到类上面的注解
			boolean isExist = c.isAnnotationPresent(Description.class);
			if(isExist){
				//拿到注解实例
				Description d = (Description) c.getAnnotation(Description.class);
				System.out.println(d.age());
			}
			
			//4。找到方法上的注解
			Method[] ms = c.getMethods();
			for(Method m : ms){
				boolean isMExist = m.isAnnotationPresent(Description.class);
				if(isMExist){
					Description d = m.getAnnotation(Description.class);
					System.out.println(d.age());
				}
			}
			//另外一种解析方法
			for(Method m : ms){
				Annotation [] as = m.getAnnotations();
				for(Annotation a : as){
					if(a instanceof Description){
						Description d = (Description) a;
						System.out.println(d.author());
					}
				}
			}
			
			Field[] field = c.getDeclaredFields();
			for (Field f : field) {
				boolean isFExist = f.isAnnotationPresent(Description.class);
				if(isFExist){
					Description o = f.getAnnotation(Description.class);
					System.out.println(o.desc());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
